package com.fhj.movie.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import com.fhj.movie.fragment.HyyToturailFragment;
import com.fhj.movie.fragment.HyyToturailFragment1;
import com.fhj.movie.fragment.HyyToturailFragment2;
import com.fhj.movie_fhj_01.R;
import com.fhj.movie_main.MainActivity;
import com.fhj.utils.SharedPreferencesUtils;



import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class HyyTasteActivity extends FragmentActivity {
	private ViewPager viewPager;
	private List<Fragment> fragment;
	private static boolean isExit=false;
	private String value;
	private static Handler  handler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			isExit= false;
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_taste);
		viewPager=(ViewPager)this.findViewById(R.id.taste_viewPager);
		fragment=new ArrayList<Fragment>();
		fragment.add(new HyyToturailFragment());
		fragment.add(new HyyToturailFragment1());
		fragment.add(new HyyToturailFragment2());
		MyAdapter adapter=new MyAdapter(getSupportFragmentManager());
		
		Map<String,String> map=(Map<String, String>) SharedPreferencesUtils.getAllSharedPreferences(this, "movie");
		value=map.get("count");
		Log.i("", "----------------------->>"+value);
		if(value==null){
			viewPager.setAdapter(adapter);
		}else if(value.equals("OK")){
			Intent intent=new Intent(HyyTasteActivity.this,MainActivity.class);
			startActivity(intent);
			this.finish();
		}
		
	}
	class MyAdapter extends FragmentPagerAdapter{

		public MyAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int arg0) {
			// TODO Auto-generated method stub
			return fragment.get(arg0);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return fragment.size();
		}
		
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO 实现两次点击退出界面
		if (keyCode==KeyEvent.KEYCODE_BACK) {
			exit();
			return  true;
		}
		return super.onKeyDown(keyCode, event);
	}
	private void exit() {
		// TODO 两次点击退出方法实现
		if (!isExit) {
			isExit=true;
			Toast.makeText(this, "再次点击退出应用", Toast.LENGTH_SHORT).show();
			handler.sendEmptyMessageDelayed(0, 2000);
		}else{
			this.finish();
		}
	}
}
